A complex number is represented by two doubles, namely, the real and imaginary parts. A complex number can be created in various ways, as shown in the examples below. The symbols i and j are used to represent the imaginary unit
[i, 2i, 2e-1i, j, 2j, 2e-1j, complex(1,2), complex(1,-2)]
ans =
Columns 1 through 3:
0.0000 + 1.0000i 0.0000 + 2.0000i 0.0000 + 0.2000i
Columns 4 through 6:
0.0000 + 1.0000i 0.0000 + 2.0000i 0.0000 + 0.2000i
Columns 7 through 8:
1.0000 + 2.0000i 1.0000 - 2.0000i
If a value is assigned to i or j, such as shown in Line 7 of the example below, it will serve as a variable in subsequent calculations (instead of the imaginary unit). To avoid careless mistakes, using complex() is the preferable way to create complex numbers.
clear
% i as imaginary unit
a = 1 + i
% 50 is assigned to i.
% i now becomes a variable.
i = 50
b = 1 + i
All variables cleared.
a =
1.0000 + 1.0000i
i =
50.000
b =
51.000